home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / texture / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.1 KB  |  253 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* texture.c
  19.  * This program reads in a single image from a .rgb file, uses the 
  20.  * utility routine gluScaleImage() to scale the image, if 
  21.  * necessary, sets up the minification and magnification filter,
  22.  * and then uses glTexImage2D() to load the image into texture 
  23.  * memory. It then applies the texture to a rectangular polygon 
  24.  * using explicit texture coordinates.
  25.  *
  26.  *    Escape Key    - exit program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <math.h>
  33. #include <stdio.h>
  34.  
  35. #include "rgbImageFile.h"    /* should be in ../../include */
  36.  
  37. /*  Function Prototypes  */
  38.  
  39. GLvoid  initgfx( GLvoid );
  40. GLvoid  animate( GLvoid );
  41. GLvoid  visibility( GLint );
  42. GLvoid  drawScene( GLvoid );
  43. GLvoid  reshape( GLsizei, GLsizei );
  44. GLvoid  keyboard( GLubyte, GLint, GLint );
  45.  
  46. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  47.  
  48. static GLuint  nearestPower( GLuint ); 
  49.  
  50. void  printHelp( char *progname );
  51.  
  52. /* Global Definitions */
  53.  
  54. #define KEY_ESC    27    /* ascii value for the escape key */
  55.  
  56. /* Global Variables */
  57.  
  58. static GLfloat swim = 1.0;
  59.  
  60. void
  61. main( int argc, char *argv[] )
  62. {
  63.     char        *imageFileName = "fish.rgba";
  64.     unsigned int     *image;
  65.     GLsizei        width, height;
  66.     GLsizei        imageWidth, imageHeight;
  67.  
  68.     glutInit( &argc, argv );
  69.  
  70.     if (argc < 2) {
  71.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  72.     } else
  73.         imageFileName = argv[1];
  74.  
  75.     fprintf(stdout, "using image %s\n\n", imageFileName );
  76.  
  77.     image = rgbReadImageFile(imageFileName, &imageWidth, &imageHeight);
  78.  
  79.     /* create a window that is 1/4 the size of the screen */
  80.  
  81.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  82.     height = glutGet( GLUT_SCREEN_HEIGHT );
  83.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  84.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  85.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  86.     glutCreateWindow( argv[0] );
  87.  
  88.     initTexture( image, imageWidth, imageHeight );
  89.     initgfx();
  90.  
  91.     glutKeyboardFunc( keyboard );
  92.     glutIdleFunc( animate );
  93.     glutVisibilityFunc( visibility );
  94.     glutReshapeFunc( reshape );
  95.     glutDisplayFunc( drawScene ); 
  96.  
  97.     printHelp( argv[0] );
  98.  
  99.     glutMainLoop();
  100. }
  101.  
  102. GLvoid
  103. printHelp( char *progname )
  104. {
  105.     fprintf(stdout, "\n%s - demonstrates basic texture mapping\n"
  106.         "Escape key     - exit the program\n\n",
  107.         progname );
  108. }
  109.  
  110. GLvoid
  111. initgfx( void )
  112. {
  113.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  114. }
  115.  
  116.  
  117. /* Compute the nearest power of 2 number that is 
  118.  * less than or equal to the value passed in. 
  119.  */
  120. static GLuint 
  121. nearestPower( GLuint value )
  122. {
  123.     int i = 1;
  124.  
  125.     if (value == 0) return -1;    /* Error! */
  126.     for (;;) {
  127.         if (value == 1) return i;
  128.         else if (value == 3) return i*4;
  129.         value >>= 1; i *= 2;
  130.     }
  131. }
  132.  
  133. GLvoid 
  134. initTexture( unsigned int *image, 
  135.     GLsizei imageWidth, GLsizei imageHeight )
  136. {
  137.     GLsizei sWidth, sHeight;
  138.     GLubyte *sImage;
  139.  
  140.     /* Find the largest power of two dimensions that are
  141.      * less than or equal to the size of the image 
  142.      */
  143.     sWidth = nearestPower( imageWidth );
  144.     sHeight = nearestPower( imageHeight );
  145.  
  146.     printf( "input image size: %dx%d\n", imageWidth, imageHeight );
  147.     printf( "scaled image size: %dx%d\n", sWidth, sHeight );
  148.  
  149.     /* scale texture image to 2^m by 2^n if necessary */
  150.     if ( sWidth == imageWidth && sHeight == imageHeight ) {
  151.         sImage = (GLubyte *) image;
  152.     } else {
  153.         sImage = (GLubyte *)malloc( sHeight*sWidth*4*sizeof( GLubyte ) );
  154.         gluScaleImage( GL_RGBA, imageWidth, imageHeight, 
  155.                 GL_UNSIGNED_BYTE, image,
  156.                 sWidth, sHeight, GL_UNSIGNED_BYTE, sImage );
  157.     }
  158.  
  159.     /* Setting the minification and magnification filters
  160.      * to nearest instead of linear, may run faster on some 
  161.      * platforms, with possibly lower quality 
  162.      */
  163.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  164.  
  165.     /* Set the minification filter to something other than 
  166.      * the default (GL_NEAREST_MIPMAP_LINEAR)
  167.      */
  168.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  169.  
  170.     /* load texture */
  171.     glTexImage2D(GL_TEXTURE_2D, 0, 4, sWidth, sHeight,
  172.             0, GL_RGBA, GL_UNSIGNED_BYTE, sImage);
  173.  
  174.     /* enable 2D texture mapping */
  175.     glEnable( GL_TEXTURE_2D );
  176. }
  177.  
  178. GLvoid 
  179. keyboard( GLubyte key, GLint x, GLint y )
  180. {
  181.     switch (key) {
  182.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  183.         exit(0);
  184.     }
  185. }
  186.  
  187. GLvoid 
  188. animate( GLvoid )
  189. {
  190.     swim = fmodf( swim + 0.1, 35.0 );
  191.  
  192.     /* Tell GLUT to redraw the scene */
  193.     glutPostRedisplay();
  194. }
  195.  
  196. GLvoid
  197. visibility( int state ) 
  198. {
  199.     if (state == GLUT_VISIBLE) {
  200.         glutIdleFunc( animate );
  201.     } else {
  202.         glutIdleFunc( NULL );
  203.     }
  204. }
  205.  
  206. GLvoid
  207. reshape( GLsizei width, GLsizei height )
  208. {
  209.     GLdouble      aspect;
  210.  
  211.     glViewport( 0, 0, width, height );
  212.  
  213.     aspect = (GLdouble) width / (GLdouble) height;
  214.  
  215.     glMatrixMode( GL_PROJECTION );
  216.     glLoadIdentity();
  217.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  218.     glMatrixMode( GL_MODELVIEW );
  219.     glLoadIdentity();
  220.     glTranslatef( 0.0, 0.0, -12.0 ); 
  221. }
  222.  
  223. GLvoid
  224. drawScene(void)
  225. {
  226.     static float v0[3] = { -1.5, -1.0, 0.0 };
  227.     static float v1[3] = {  1.5, -1.0, 0.0 };
  228.     static float v2[3] = {  1.5,  1.0, 0.0 };
  229.     static float v3[3] = { -1.5,  1.0, 0.0 };
  230.  
  231.     static float t0[2] = { 0.0, 0.0 };
  232.     static float t1[2] = { 1.0, 0.0 };
  233.     static float t2[2] = { 1.0, 1.0 };
  234.     static float t3[2] = { 0.0, 1.0 };
  235.  
  236.     glClear( GL_COLOR_BUFFER_BIT );
  237.  
  238.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  239.     glPushMatrix();
  240.         glTranslatef( -6.0 + swim, 0.2*sinf(swim * 3.0), -swim );
  241.     
  242.         glBegin( GL_QUADS );
  243.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  244.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  245.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  246.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  247.         glEnd();
  248.  
  249.     glPopMatrix();
  250.  
  251.     glutSwapBuffers();
  252. }
  253.